﻿using AppMesPlc.DAL;
using Newtonsoft.Json.Linq;
using SqlSugar;

namespace AppMesPlc
{
    public static class SqlSugarHelper
    {
        public static SqlSugarClient Db { get; private set; }

        static SqlSugarHelper()
        {
            // 从 appSettings.json 文件中获取连接字符串
            string connectionString = QueryDal.GetAppsettings()["dbConnect"].ToString();
            // 如果获取的连接字符串为空，抛出异常
            if (string.IsNullOrEmpty(connectionString))
            {
                throw new InvalidOperationException("数据库连接字符串未配置。");
            }

            // 创建数据库对象
            Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = connectionString,
                DbType = DbType.SqlServer,
                IsAutoCloseConnection = true
            },
            db =>
            {
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    // 输出 SQL 到控制台
                    Console.WriteLine(sql);
                };
            });
        }
    }
}
